package org.jeecg.modules.articles.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.articles.entity.BlogArticles;
import org.jeecg.modules.articles.service.BlogArticlesService;
import org.jeecg.config.shiro.IgnoreAuth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: blog_articles
 * @Author: jeecg-boot
 * @Date: 2025-06-30
 * @Version: V1.0
 */
@Tag(name = "blog_articles")
@RestController
@RequestMapping("/blog/articles")
@Slf4j
public class BlogArticlesController extends JeecgController<BlogArticles, BlogArticlesService> {
    @Autowired
    private BlogArticlesService blogArticlesService;

    /**
     * 分页列表查询
     *
     * @param blogArticles
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "blog_articles-分页列表查询")
    @Operation(summary = "blog_articles-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<BlogArticles>> queryPageList(BlogArticles blogArticles,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<BlogArticles> queryWrapper = QueryGenerator.initQueryWrapper(blogArticles, req.getParameterMap());
        queryWrapper.eq("del_flag", 0);
        Page<BlogArticles> page = new Page<BlogArticles>(pageNo, pageSize);
        IPage<BlogArticles> pageList = blogArticlesService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 公开的文章列表查询（无需权限）
     *
     * @param pageNo
     * @param pageSize
     * @param categoryId 分类ID
     * @param title      标题搜索
     * @return
     */
    @Operation(summary = "公开的文章列表查询")
    @GetMapping(value = "/public/list")
    @IgnoreAuth
    public Result<IPage<BlogArticles>> getPublicArticles(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "categoryId", required = false) String categoryId,
            @RequestParam(name = "title", required = false) String title) {
        QueryWrapper<BlogArticles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1) // 只查询已发布的文章
                .eq("del_flag", 0)
                .orderByDesc("create_time");

        // 分类筛选
        if (categoryId != null && !categoryId.isEmpty()) {
            queryWrapper.eq("category_id", categoryId);
        }

        // 标题搜索
        if (title != null && !title.isEmpty()) {
            queryWrapper.like("title", title);
        }

        Page<BlogArticles> page = new Page<BlogArticles>(pageNo, pageSize);
        IPage<BlogArticles> pageList = blogArticlesService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 公开的文章详情查询（无需权限）
     *
     * @param id
     * @return
     */
    @Operation(summary = "公开的文章详情查询")
    @GetMapping(value = "/public/detail")
    @IgnoreAuth
    public Result<BlogArticles> getPublicArticleDetail(@RequestParam(name = "id", required = true) String id) {
        QueryWrapper<BlogArticles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id)
                .eq("status", 1); // 只查询已发布的文章
        BlogArticles article = blogArticlesService.getOne(queryWrapper);
        if (article == null) {
            return Result.error("文章不存在或未发布");
        }
        return Result.OK(article);
    }

    /**
     * 添加
     *
     * @param blogArticles
     * @return
     */
    @AutoLog(value = "blog_articles-添加")
    @Operation(summary = "blog_articles-添加")
    @RequiresPermissions("articles:blog_articles:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody BlogArticles blogArticles) {
        blogArticlesService.save(blogArticles);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param blogArticles
     * @return
     */
    @AutoLog(value = "blog_articles-编辑")
    @Operation(summary = "blog_articles-编辑")
    @RequiresPermissions("articles:blog_articles:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody BlogArticles blogArticles) {
        blogArticlesService.updateById(blogArticles);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "blog_articles-通过id删除")
    @Operation(summary = "blog_articles-通过id删除")
    @RequiresPermissions("articles:blog_articles:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        BlogArticles blog = blogArticlesService.getById(id);

        blog.setDelFlag(1);

        blogArticlesService.updateById(blog);

        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "blog_articles-批量删除")
    @Operation(summary = "blog_articles-批量删除")
    @RequiresPermissions("articles:blog_articles:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.blogArticlesService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "blog_articles-通过id查询")
    @Operation(summary = "blog_articles-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<BlogArticles> queryById(@RequestParam(name = "id", required = true) String id) {
        BlogArticles blogArticles = blogArticlesService.getById(id);
        if (blogArticles == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(blogArticles);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param blogArticles
     */
    @RequiresPermissions("articles:blog_articles:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BlogArticles blogArticles) {
        return super.exportXls(request, blogArticles, BlogArticles.class, "blog_articles");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("articles:blog_articles:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BlogArticles.class);
    }

}
